This Jupyter Notebook is created using Python version 3.8.2
The following code will help you set up the broker connection with Zerodha, which will be used by all the recipes in this chapter. Please make sure you have followed these steps before trying out any recipe.
from pyalgotrading.broker.broker_connection_zerodha import BrokerConnectionZerodha
# Get the api_key and api_secret from broker. These are unique to you and will be used by the broker to identify your demat account.
api_key = "5mrqgbb6ms4s73am"
api_secret = "dbrqbp3ketmvn2lav17ajwmfgy2i96w2"
broker_connection = BrokerConnectionZerodha(api_key, api_secret)
# Get the request token from the above URL
request_token = "A6qlnYlXru9l87vK2r10EjWq7qk7uU2R"
broker_connection.set_access_token(request_token)
# Fetch historical data for an instrument
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data = broker_connection.get_historical_data(instrument=instrument,
candle_interval='minute',
start_date='2020-01-01',
end_date='2020-01-01')
historical_data
historical_data.columns
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
# A 'Green' Japanese Candle
candle_green = historical_data.iloc[:1,:] # Only 1st ROW of historical data
plot_candlestick_chart(candle_green, PlotType.JAPANESE, "A 'Green' Japanese Candle")
# A Red Japanese Candle
candle_red = historical_data.iloc[1:2,:] # Only 2nd ROW of historical data
plot_candlestick_chart(candle_red, PlotType.JAPANESE, "A 'Red' Japanese Candle")
plot_candlestick_chart(historical_data,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Minute')
instrument2 = broker_connection.get_instrument('NSE', 'INFY')
historical_data = broker_connection.get_historical_data(instrument2,
'minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:INFY | 1st Jan, 2020 | Candle Interval: 1 Minute')
instrument3 = broker_connection.get_instrument('NSE', 'ICICIBANK')
historical_data = broker_connection.get_historical_data(instrument3,
'minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:ICICIBANK | 1st Jan, 2020 | Candle Size: 1 Minute')
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
# Fetch an instrument
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument,
'minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_1minute,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Minute')
historical_data_3minutes = broker_connection.get_historical_data(instrument,
'3minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_3minutes,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 3 Minutes')
historical_data_5minutes = broker_connection.get_historical_data(instrument,
'5minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_5minutes,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 5 Minutes')
historical_data_10minutes = broker_connection.get_historical_data(instrument,
'10minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_10minutes,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 10 Minutes')
historical_data_15minutes = broker_connection.get_historical_data(instrument,
'15minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_15minutes,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 15 Minutes')
historical_data_30minutes = broker_connection.get_historical_data(instrument,
'30minute',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_30minutes,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 30 Minutes')
historical_data_1hour = broker_connection.get_historical_data(instrument,
'hour',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_1hour,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: 1 Hour')
historical_data_day = broker_connection.get_historical_data(instrument,
'day',
'2020-01-01',
'2020-01-01')
plot_candlestick_chart(historical_data_day,
PlotType.JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | NSE:TATASTEEL | 1st Jan, 2020 | Candle Interval: Day')
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.linebreak import Linebreak
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument,
'minute',
'2019-12-01',
'2019-12-31')
historical_data_1minute_linebreak = Linebreak(historical_data_1minute)
historical_data_1minute_linebreak
# A 'Green' Linebreak Candle
candle_green_linebreak = historical_data_1minute_linebreak.iloc[1:2,:] # Only 2nd ROW of historical data
plot_candlestick_chart(candle_green_linebreak,
PlotType.LINEBREAK,
"A 'Green' Linebreak Candle")
# A Red Linebreak Candle
candle_red_linebreak = historical_data_1minute_linebreak.iloc[:1,:] # Only 1st ROW of historical data
plot_candlestick_chart(candle_red_linebreak,
PlotType.LINEBREAK,
"A 'Red' Linebreak Candle")
plot_candlestick_chart(historical_data_1minute_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Minute', True)
historical_data_3minutes = broker_connection.get_historical_data(instrument,
'3minute',
'2019-12-01',
'2019-12-31')
historical_data_3minutes_linebreak = Linebreak(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 Minutes', True)
historical_data_5minutes = broker_connection.get_historical_data(instrument,
'5minute',
'2019-12-01',
'2020-01-10')
historical_data_5minutes_linebreak = Linebreak(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 Minutes', True)
historical_data_10minutes = broker_connection.get_historical_data(instrument,
'10minute',
'2019-12-01',
'2020-01-10')
historical_data_10minutes_linebreak = Linebreak(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 Minutes', True)
historical_data_15minutes = broker_connection.get_historical_data(instrument,
'15minute',
'2019-12-01',
'2020-01-10')
historical_data_15minutes_linebreak = Linebreak(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 Minutes', True)
historical_data_30minutes = broker_connection.get_historical_data(instrument,
'30minute',
'2019-12-01',
'2020-01-10')
historical_data_30minutes_linebreak = Linebreak(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 Minutes', True)
historical_data_1hour = broker_connection.get_historical_data(instrument,
'hour',
'2019-12-01',
'2020-01-10')
historical_data_1hour_linebreak = Linebreak(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
historical_data_day = broker_connection.get_historical_data(instrument,
'day',
'2019-12-01',
'2020-01-10')
historical_data_day_linebreak = Linebreak(historical_data_day)
plot_candlestick_chart(historical_data_day_linebreak,
PlotType.LINEBREAK,
'Historical Data | Linebreak Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Day', True)
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.renko import Renko
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument,
'minute',
'2019-12-01',
'2020-01-10')
historical_data_1minute_renko = Renko(historical_data_1minute)
historical_data_1minute_renko
# A Green Renko Candle
candle_green_renko = historical_data_1minute_renko.iloc[2:3,:] # Only 3rd ROW of historical data
plot_candlestick_chart(candle_green_renko,
PlotType.RENKO,
"A Green 'Renko' Candle")
# A Red Renko Candles
candle_red_renko = historical_data_1minute_renko.iloc[1:2,:] # Only 2nd ROW of historical data
plot_candlestick_chart(candle_red_renko,
PlotType.RENKO,
"A 'Red' Renko Candle")
plot_candlestick_chart(historical_data_1minute_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Minute', True)
historical_data_3minutes = broker_connection.get_historical_data(instrument,
'3minute',
'2019-12-01',
'2019-12-31')
historical_data_3minutes_renko = Renko(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 Minutes', True)
historical_data_5minutes = broker_connection.get_historical_data(instrument,
'5minute',
'2019-12-01',
'2019-12-31')
historical_data_5minutes_renko = Renko(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 Minutes', True)
historical_data_10minutes = broker_connection.get_historical_data(instrument,
'10minute',
'2019-12-01',
'2019-12-31')
historical_data_10minutes_renko = Renko(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 Minutes', True)
historical_data_15minutes = broker_connection.get_historical_data(instrument,
'15minute',
'2019-12-01',
'2019-12-31')
historical_data_15minutes_renko = Renko(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 Minutes', True)
historical_data_30minutes = broker_connection.get_historical_data(instrument,
'30minute',
'2019-12-01',
'2019-12-31')
historical_data_30minutes_renko = Renko(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 Minutes', True)
historical_data_1hour = broker_connection.get_historical_data(instrument,
'hour',
'2019-12-01',
'2019-12-31')
historical_data_1hour_renko = Renko(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
historical_data_day = broker_connection.get_historical_data(instrument,
'day',
'2019-12-01',
'2019-12-31')
historical_data_day_renko = Renko(historical_data_day)
plot_candlestick_chart(historical_data_day_renko,
PlotType.RENKO,
'Historical Data | Renko Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: Day', True)
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
from pyalgotrading.utils.candlesticks.heikinashi import HeikinAshi
instrument = broker_connection.get_instrument('NSE', 'TATASTEEL')
historical_data_1minute = broker_connection.get_historical_data(instrument,
'minute',
'2019-12-01',
'2019-12-31')
historical_data_1minute_heikinashi = HeikinAshi(historical_data_1minute)
historical_data_1minute_heikinashi
# A 'Green' HeikinAshi Candle
candle_green_heikinashi = historical_data_1minute_heikinashi.iloc[2:3,:] # Only 3rd ROW of historical data
plot_candlestick_chart(candle_green_heikinashi,
PlotType.HEIKINASHI,
"A 'Green' HeikinAshi Candle")
# A 'Red' HeikinAshi Candle
candle_red_heikinashi = historical_data_1minute_heikinashi.iloc[4:5,:] # Only 1st ROW of historical data
plot_candlestick_chart(candle_red_heikinashi,
PlotType.HEIKINASHI,
"A 'Red' HeikinAshi Candle")
plot_candlestick_chart(historical_data_1minute_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 minute', True)
historical_data_3minutes = broker_connection.get_historical_data(instrument,
'3minute',
'2019-12-01',
'2019-12-31')
historical_data_3minutes_heikinashi = HeikinAshi(historical_data_3minutes)
plot_candlestick_chart(historical_data_3minutes_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 3 minutes', True)
historical_data_5minutes = broker_connection.get_historical_data(instrument,
'5minute',
'2019-12-01',
'2019-12-31')
historical_data_5minutes_heikinashi = HeikinAshi(historical_data_5minutes)
plot_candlestick_chart(historical_data_5minutes_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 5 minutes', True)
historical_data_10minutes = broker_connection.get_historical_data(instrument,
'10minute',
'2019-12-01',
'2019-12-31')
historical_data_10minutes_heikinashi = HeikinAshi(historical_data_10minutes)
plot_candlestick_chart(historical_data_10minutes_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 10 minutes', True)
historical_data_15minutes = broker_connection.get_historical_data(instrument,
'15minute',
'2019-12-01',
'2019-12-31')
historical_data_15minutes_heikinashi = HeikinAshi(historical_data_15minutes)
plot_candlestick_chart(historical_data_15minutes_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 15 minutes', True)
historical_data_30minutes = broker_connection.get_historical_data(instrument,
'30minute',
'2019-12-01',
'2019-12-31')
historical_data_30minutes_heikinashi = HeikinAshi(historical_data_30minutes)
plot_candlestick_chart(historical_data_30minutes_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 30 minutes', True)
historical_data_1hour = broker_connection.get_historical_data(instrument,
'hour',
'2019-12-01',
'2019-12-31')
historical_data_1hour_heikinashi = HeikinAshi(historical_data_1hour)
plot_candlestick_chart(historical_data_1hour_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: 1 Hour', True)
historical_data_day = broker_connection.get_historical_data(instrument,
'day',
'2019-12-01',
'2019-12-31')
historical_data_day_heikinashi = HeikinAshi(historical_data_day)
plot_candlestick_chart(historical_data_day_heikinashi,
PlotType.HEIKINASHI,
'Historical Data | Heikin-Ashi Candlesticks Pattern | NSE:TATASTEEL | Dec, 2019 | Candle Interval: Day', True)
from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
import quandl
facebook = quandl.get('WIKI/FB',
start_date='2015-1-1',
end_date='2015-3-31')
plot_candlestick_chart(facebook,
PlotType.QUANDL_JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | FACEBOOK | Jan-March 2015 | Candle Interval: Day', True)
amazon = quandl.get('WIKI/AMZN',
start_date='2015-1-1',
end_date='2015-3-31')
plot_candlestick_chart(amazon,
PlotType.QUANDL_JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | AMAZON | Jan-March 2015 | Candle Interval: Day', True)
apple = quandl.get('WIKI/AAPL',
start_date='2015-1-1',
end_date='2015-3-31')
plot_candlestick_chart(apple,
PlotType.QUANDL_JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | APPLE | Jan-March 2015 | Candle Interval: Day', True)
microsoft = quandl.get('WIKI/MSFT',
start_date='2015-1-1',
end_date='2015-3-31')
plot_candlestick_chart(microsoft,
PlotType.QUANDL_JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | MICROSOFT | Jan-March 2015 | Candle Interval: Day', True)
google = quandl.get('WIKI/GOOGL',
start_date='2015-1-1',
end_date='2015-3-31')
plot_candlestick_chart(google,
PlotType.QUANDL_JAPANESE,
'Historical Data | Japanese Candlesticks Pattern | GOOGLE | Jan-March 2015 | Candle Interval: Day', True)